package com.fnic.wifi.threadLocal;

public class ThreadLocalDemo implements Runnable {

	private final static ThreadLocal<Integer> tLocal = new ThreadLocal<Integer>();

	public static void main(String[] agrs) {
		ThreadLocalDemo td = new ThreadLocalDemo();
		Thread t1 = new Thread(td, "t1");
		Thread t2 = new Thread(td, "t2");
		t1.start();
		t2.start();
	}

	/**
	 * 示例业务方法
	 */
	public void test() {
		while (true) {
			try {
				Integer b = (Integer) tLocal.get();
				if (b == null) {
					b=0;
					tLocal.set(b);
				}
				String currentThreadName = Thread.currentThread().getName();
				System.out.println("thread " + currentThreadName + " set value to:" + b++);
				tLocal.set(b);
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void run() {
		test();
	}

}